/**
 * 
 */
package com.objectdraw.server.commands;

import com.objectdraw.server.CommunicationServer;
import com.objectdraw.server.ObjectDrawServer;
import com.objectdraw.server.data.Action;
import com.objectdraw.server.data.RequestData;
import com.objectdraw.server.data.ResponseData;

/**
 * Processes a mouse command from the client.
 * 
 * @author jgulik, cfruehan, Harrison
 *
 */
public class MouseCommand extends Command {
	static final private String myCommand = "mouse";
	/* (non-Javadoc)
	 * @see com.objectdraw.server.commands.Command#processRequest(com.objectdraw.server.RequestData, com.objectdraw.server.ResponseData)
	 */
	/**
	 * Takes a message with the command "mouse" and sends the action to the current tool.  Also broadcasts
	 * the message to all listening clients.
	 */
	@Override
	public void processRequest(RequestData request, ResponseData response) {
		if(myCommand.equalsIgnoreCase(request.getCommand())){
			/**
			 * Mouse Message:
			 * mouse <event> <x coord> <y coord>
			 * <events>:
			 * pressed, released, dragged
			 */
			System.out.println(myCommand +" Command");
			if(request.getParams() != null){
				String[] params = request.getParams().split(" ");
				if(params.length > 2){
					ObjectDrawServer server = ObjectDrawServer.getInstance();
					String event = params[0];
					String xcord = params[1];
					String ycord = params[2];
					
					Action act = new Action();
					act.setArguments(xcord + " " + ycord);
					act.setAction(myCommand);
					act.setEvent(event);
					server.addCanvasAction(act);
					
					//Broadcast the new thing
					response.addResponse(myCommand + " " + event + " " + xcord + " " + ycord);
					CommunicationServer.broadcast(response, server.getViewingClientSockets());
				}
			}
		}
		
		//Pass the request on to the next person in line
		if(this.successor != null){
			this.successor.processRequest(request,response);
		}

	}

}
